#!/bin/bash

menu(){
    echo "1、创建用户"
    echo "2、删除指定用户"
    echo "3、修改指定用户（用户id，用户属组，用户家目录）"
    echo "4、删除指定用户"
}

add(){
    read -p "输入用户名:" username
    useradd $username &>/dev/null && echo "创建成功" || echo "创建失败"
}

del(){
    read -p "输入用户名:" username
    userdel -r $username &>/dev/null && echo "删除成功" || echo "用户不存在"
}

modify(){
    read -p "输入用户名:" username
    id $username &>/dev/null
    if [[ $? = 0 ]]
    then
        read -p "输入uid:" uid
        read -p "输入gid:" gid
        read -p "输入家目录:" home
        usermod -u $uid -g $gid -d $home $username 2>/dev/null && echo "修改成功" || "修改失败"
    else
        echo "用户不存在"
    fi
}

while :
do
    menu
    read -p "请输入1-4：" option
    case $option in
    1)
        add
        ;;
    2)
        del
        ;;
    3)
        modify
        ;;
    4)
        del
        ;;
    *)
        echo "输入不合法"
    esac
done

